package com.example.service;

import com.baomidou.mybatisplus.service.IService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.entity.Account;
import com.example.entity.Payment;
import com.example.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class IAccount extends ServiceImpl<AccountMapper, Account> implements IService<Account> {

    @Autowired
    AccountMapper accountMapper;


    @Transactional
    public void updateAccount(Payment payment) throws Exception {
        Account account = new Account();
        account.setUserId(payment.getUserId());
        Account account1 = accountMapper.selectOne(account);
        if (account1 != null && account1.getBalance() >= payment.getAmount()) {
            account1.setBalance(account1.getBalance() - payment.getAmount());
            accountMapper.updateById(account1);
        } else {
            throw new Exception(" update account  fail");
        }
    }
}
